package com.jr.controller;

import com.jr.pojo.Pet;
import com.jr.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/pet")
@CrossOrigin(origins = "*")
public class PetController {

    @Autowired
    private PetService petService;

    @PostMapping("/add")
    public boolean addPet(@RequestBody Pet pet) {
        System.out.println("添加宠物信息");
        return petService.addPet(pet);
    }

    @GetMapping("/list")
    public Map<String, Object> getPetsByOwnerId(@RequestParam Integer ownerId, 
                                               @RequestParam(defaultValue = "1") Integer pageIndex,
                                               @RequestParam(defaultValue = "5") Integer pageSize) {
        System.out.println("查询宠物列表 - ownerId: " + ownerId + ", pageIndex: " + pageIndex + ", pageSize: " + pageSize);
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取总数
            int totalCount = petService.getPetCountByOwnerId(ownerId);
            System.out.println("宠物总数: " + totalCount);
            
            // 计算总页数
            int pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
            
            // 获取分页数据
            ArrayList<Pet> pets = petService.getPetsByPage(ownerId, pageIndex, pageSize);
            System.out.println("当前页宠物数量: " + pets.size());
            
            for (Pet pet : pets) {
                System.out.println("宠物信息: " + pet.toString());
            }
            
            result.put("success", true);
            result.put("pets", pets);
            result.put("count", pets.size());
            result.put("totalCount", totalCount);
            result.put("pageCount", pageCount);
            result.put("currentPage", pageIndex);
            result.put("pageSize", pageSize);
            result.put("message", "查询成功");
            
        } catch (Exception e) {
            System.err.println("查询宠物列表失败: " + e.getMessage());
            e.printStackTrace();
            
            result.put("success", false);
            result.put("pets", new ArrayList<>());
            result.put("count", 0);
            result.put("totalCount", 0);
            result.put("pageCount", 0);
            result.put("currentPage", pageIndex);
            result.put("pageSize", pageSize);
            result.put("message", "查询失败: " + e.getMessage());
        }
        
        System.out.println("返回的分页数据: " + result);
        return result;
    }

    @GetMapping("/detail")
    public Pet getPetById(@RequestParam Integer petId) {
        System.out.println("查询宠物详情");
        return petService.getPetById(petId);
    }

    @PutMapping("/update")
    public boolean updatePet(@RequestBody Pet pet) {
        System.out.println("更新宠物信息");
        return petService.updatePet(pet);
    }

    @DeleteMapping("/delete")
    public boolean deletePet(@RequestParam Integer petId) {
        System.out.println("删除宠物");
        return petService.deletePet(petId);
    }
}
